## -*- coding: utf-8 -*-
from nltk.tokenize import sent_tokenize as en_sent_tokenize
import re

def is_chinese(string):
    """
    检查整个字符串是否包含中文
    :param string: 需要检查的字符串
    :return: bool
    """
    for ch in string:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True

    return False


def zh_cut_sentences(content):
    # 结束符号，包含中文和英文的
    end_flag = ['?', '!', '.', '？', '！', '。', '…']

    content_len = len(content)
    sentences = []
    tmp_char = ''
    for idx, char in enumerate(content):
        # 拼接字符
        tmp_char += char

        # 判断是否已经到了最后一位
        if (idx + 1) == content_len:
            sentences.append(tmp_char)
            break

        # 判断此字符是否为结束符号
        if char in end_flag:
            # 再判断下一个字符是否为结束符号，如果不是结束符号，则切分句子
            next_idx = idx + 1
            if not content[next_idx] in end_flag:
                sentences.append(tmp_char)
                tmp_char = ''

    return sentences


def sent_tokenize(text, language="english"):
    if is_chinese(text):
        sentence = zh_cut_sentences(text)
    else:
        try:
            sentence = en_sent_tokenize(text,language)
        except Exception as e:
            sentence = []

    return sentence



def check_noise(column):
    pattern1 = '{+\s+'  #whitespace after opening bracket
    pattern2 = '\s+}+' #whitespace before closing bracket
    pattern3 = '\s+\|' #whitespace before pipe operator
    pattern4 = '\|\s+' #whitespace after pipe operator
    pattern5 = '([^\w^\s]+)[\s]*\|' #punctuation before pipe operator
    pattern6 = '\|([^\w]+[\s]*)' #punctuation after pipe operator
    problems = []

    for index, utterance in column.items():
        if type(utterance) == str:   #check that row contains string
            slots =  re.findall(r'\{[^\{\}]*\}',utterance)   #find all slots in string
            for slot in slots:
                match1 = re.findall(pattern1,slot)
                match2 = re.findall(pattern2,slot)
                match3 = re.findall(pattern3,slot)
                match4 = re.findall(pattern4,slot)
                #print(pattern5)
                if re.findall(pattern5,slot) != ['%'] and re.findall(pattern5,slot) != ['$'] and re.findall(pattern5,slot) != ['€']:
                    match5 = re.findall(pattern5,slot)
                match6 = re.findall(pattern6,slot)

                if match1:
                    num_of_spaces = len(match1[0])-1 #-1 because the bracket counts as a character
                    if num_of_spaces >=1:
                        error = '%d space%s found after opening bracket' % (num_of_spaces, "s"[num_of_spaces==1:])
                        problems.append([index, slot, error])
                if match2:
                    num_of_spaces = len(match2[0])-1 #-1 because the bracket counts as a character
                    if num_of_spaces >=1:
                        error = '%d space%s found before closing bracket' % (num_of_spaces, "s"[num_of_spaces==1:])
                        problems.append([index, slot, error])
                if match3:
                    num_of_spaces = len(match3[0])-1 #-1 because the bracket counts as a character
                    if num_of_spaces >=1:
                        error = '%d space%s found before pipe symbol' % (num_of_spaces, "s"[num_of_spaces==1:])
                        problems.append([index, slot, error])
                if match4:
                    num_of_spaces = len(match4[0])-1 #-1 because the bracket counts as a character
                    if num_of_spaces >=1:
                        error = '%d space%s found after pipe symbol' % (num_of_spaces, "s"[num_of_spaces==1:])
                        problems.append([index, slot, error])
                if match5:
                    num_of_punct = len(match5[0])
                    if num_of_punct >=1:
                        error = '%d punctuation%s found before pipe operator' % (num_of_punct, "s"[num_of_punct==1:])
                        problems.append([index, slot, error])
                if match6:
                    num_of_punct = len(match6[0])
                    if num_of_punct >=1:
                        error = '%d punctuation%s found after pipe operator' % (num_of_punct, "s"[num_of_punct==1:])
                        problems.append([index, slot, error])
    return problems